//
//  MainTabBarController.swift
//  swiftDemo
//
//  Created by Gerry on 2021/10/27.
//

import UIKit

class MainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = .white
        initTabBar()
    }
    

    func initTabBar() {
        let home = HomeViewController()
        home.tabBarItem.title = "首页"
        home.tabBarItem.image = UIImage(named: "tabbar_home")
        home.tabBarItem.selectedImage = UIImage(named: "tabbar_home_select")
        
        let category = CategoryViewController()
        category.tabBarItem.title = "分类"
        category.tabBarItem.image = UIImage(named: "tabbar_category")
        category.tabBarItem.selectedImage = UIImage(named: "tabbar_category_select")
        
        let message = MessageViewController()
        message.tabBarItem.title = "消息"
        message.tabBarItem.image = UIImage(named: "tabbar_msg")
        message.tabBarItem.selectedImage = UIImage(named: "tabbar_msg_select")
        
        let cart = CartViewController()
        cart.tabBarItem.title = "购物车"
        cart.tabBarItem.image = UIImage(named: "tabbar_shopping")
        cart.tabBarItem.selectedImage = UIImage(named: "tabbar_shopping_select")
        
        let my = MyViewController()
        my.tabBarItem.title = "我的"
        my.tabBarItem.image = UIImage(named: "tabbar_my")
        my.tabBarItem.selectedImage = UIImage(named: "tabbar_my_select")
        
        self.viewControllers = [home, category, message, cart, my]
        setTabBarItemAttributes(bgColor: UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1))

    }
    

    func setTabBarItemAttributes(fontName: String = "PingFangSC-Regular",
                                     fontSize: CGFloat = 12,
                                     normalColor: UIColor = .gray,
                                     selectedColor: UIColor = .red,
                                     bgColor: UIColor = .white) {
            // tabBarItem 文字大小
            var attributes: [NSAttributedString.Key: Any] = [.font: UIFont(name: fontName, size: fontSize)!]
            
            // tabBarItem 文字默认颜色
            attributes[.foregroundColor] = normalColor
            UITabBarItem.appearance().setTitleTextAttributes(attributes, for: .normal)
            
            // tabBarItem 文字选中颜色
            attributes[.foregroundColor] = selectedColor
            UITabBarItem.appearance().setTitleTextAttributes(attributes, for: .selected)
            
            // tabBar 文字、图片 统一选中高亮色
            tabBar.tintColor = selectedColor
            
            // tabBar 背景色
            tabBar.barTintColor = bgColor
        
        
        }
}
